<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--用户数据-->
      <el-col :span="24" :xs="24">
        <el-form
          :model="queryParams"
          ref="queryRef"
          :inline="true"
          v-show="showSearch"
          label-width="68px"
        >
          <el-form-item label="文件名称" prop="userName">
            <el-input
              v-model="queryParams.fileName"
              placeholder="请输入文件名称"
              clearable
              style="width: 240px"
              @keyup.enter="handleQuery"
            />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="text" plain icon="Plus" @click="handleAdd"
              >新增</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="text"
              style="color: red"
              icon="Edit"
              :disabled="single"
              @click="handleUpdate"
              >修改</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="Delete"
              :disabled="multiple"
              @click="handleDelete"
              >删除</el-button
            >
          </el-col>

          <right-toolbar
            v-model:showSearch="showSearch"
            @queryTable="getList"
          ></right-toolbar>
        </el-row>

        <el-table
          v-loading="loading"
          :data="dataList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column
            label="文件名称"
            align="center"
            key="fileName"
            prop="fileName"
          />
          <el-table-column
            label="文件上传时间"
            align="center"
            key="createTime"
            prop="createTime"
            :show-overflow-tooltip="true"
          />

          <el-table-column
            label="更新责任人"
            align="center"
            key="chargeName"
            prop="chargeName"
            :show-overflow-tooltip="true"
          ></el-table-column>
          <el-table-column
            label="上次更新人员"
            align="center"
            key="updateName"
            prop="updateName"
            width="120"
          />
          <el-table-column
            label="备注信息"
            prop="remarksInfo"
            align="center"
            key="status"
          >
          </el-table-column>

          <el-table-column
            label="操作"
            align="center"
            width="250"
            class-name="small-padding fixed-width"
          >
            <template #default="scope">
              <el-button
                type="text"
                icon="download"
                @click="downLoadFile(scope.row)"
                >下载</el-button
              >
              <el-button
                type="text"
                icon="Edit"
                @click="handleUpdate(scope.row)"
                >修改</el-button
              >

              <el-button
                type="text"
                icon="Delete"
                style="color: red"
                @click="handleDelete(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="total > 0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改用户配置对话框 -->
    <el-dialog :title="title" v-model="open" width="700px" append-to-body>
      <el-form :model="form" :rules="rules" ref="userRef" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="文件名称" prop="fileName">
              <el-input
                v-model="form.fileName"
                placeholder="上传文件后自动填写"
                maxlength="30"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="更新责任人" prop="chargeName">
              <el-input
                v-model="form.chargeName"
                placeholder="请输入内容"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input
                v-model="form.remarksInfo"
                type="textarea"
                placeholder="请输入内容"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="文件上传" prop="fileId">
              <FileUpload
                :limit="1"
                :fileSize="30"
                :fileType="['doc', 'docx', 'pdf']"
                v-model="files"
              ></FileUpload>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="User">
import FileUpload from "@/components/FileUpload";
import { baozeren } from "@/api/hdanger/baozeren/index.js";

const { proxy } = getCurrentInstance();

const dataList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const files = ref([]);
function downLoadFile(row) {
  window.downLoadFile(row.fileUrl, row.fileName);
}
watch(
  () => files,
  (newData) => {
    console.log(newData);
    if (newData.value.length === 0) {
      form.value.fileName = "";
      form.value.fileId = "";
      form.value.fileUrl = "";
    } else {
      form.value.fileName = newData.value[0].name;
      form.value.fileId = newData.value[0].id;
      form.value.fileUrl = newData.value[0].url;
    }
  },
  {
    deep: true,
  }
);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    fileName: undefined,
  },
  rules: {
    chargeName: [
      { required: true, message: "责任人不能为空", trigger: "blur" },
    ],
    fileId: [{ required: true, message: "请上传文件", trigger: "blur" }],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询用户列表 */
function getList() {
  loading.value = true;
  baozeren
    .query({
      ...queryParams.value,
    })
    .then((res) => {
      loading.value = false;
      dataList.value = res.rows;
      total.value = res.total;
    });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  handleQuery();
}
/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal
    .confirm('是否确认删除"' + row.fileName + '"的数据项？')
    .then(function () {
      return baozeren.del(row);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 选择条数  */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.userId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 重置操作表单 */
function reset() {
  form.value = {};
  files.value = [];
}
/** 取消按钮 */
function cancel() {
  open.value = false;
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  form.value = {};

  open.value = true;
  title.value = "新增";
}
/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  form.value = row;
  files.value = [
    {
      name: row.fileName,
      id: row.fileId,
      url: row.fileUrl,
    },
  ];
  open.value = true;
  title.value = "修改";
}
/** 提交按钮 */
const userRef = ref();
function submitForm() {
  userRef.value.validate((valid) => {
    if (valid) {
      if (form.value.id != undefined) {
        baozeren.update(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        baozeren.add(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

getList();
</script>
